package roborally.script.nonConditionalCommand;

import roborally.model.Robot;
import roborally.script.Command;
import roborally.script.NonConditionalCommand;
import be.kuleuven.cs.som.annotate.*;
import be.kuleuven.cs.som.taglet.*;

/**
 * @author Joren VERSPEURT
 * @author Mathieu REYMOND
 * @version 3.0
 * 
 * The class representing a Seq command. This Command is composed by a number of sub-commands.
 */
public class Seq extends NonConditionalCommand {

	/**
	 * Create a new ConditionalCommand with a given Robot, a Condition and a number of sub-Commands.
	 * @param robot The Robot this Command will apply to.
	 * @param commands The Commands this ComposedCommand will be composed of.
	 */
	public Seq(Robot robot, Command... commands) {
		super(robot, commands);
	}

	/**
	 * Execute the next sub-command of this Seq-command.
	 * When all the sub-commands are fully executed, set this Command as <code>executed == true</code>
	 */
	@Override
	public void step() {
		
		for(Command command : getCommands()) {
			if(!command.isExecuted()) {
				command.step();
				break;
			}
		}
		boolean finished = true;
		for(Command command : getCommands()) {
			if(!command.isExecuted()) {
				finished = false;
				break;
			}
		}
		if(finished) {
			setExecuted(true);
		}
		
	}
	
	/**
	 * Set the executed state of this Command and all it's sub-Commands to <code> state </code>.
	 * @param state The state to set this Command's and its sub-Commands state to.
	 */
	@Override
	public void setExecuted(boolean state) {
		super.setExecuted(state);
		for(Command command : getCommands()) {
			command.setExecuted(state);
		}
	}
	
	/**
	 * The script word for this command.
	 */
	@Override
	public String toString() {
		return "seq";
	}
}
